// components/label-select/index.js
import { getClassColor, isNull } from '../../utils/index'
import api from '../../api/index'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
		list:{
			type: Array
		},
		dictType: String,
		values: {
			type: Array
		},
		value: String,
		multiple: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: ''
  },
	created: function(){
		console.log("dictType" + this.data.dictType)
		let lst = this.data.list
		if(this.$isNull(this.data.dictType)){

			for(const i in lst){
				lst[i].color = getClassColor(lst[i].listClass)
				lst[i].list[i].checked = false
				if(this.data.multiple){
					for(const c in this.data.values){
						if(lst[i].dictValue == this.data.values[c]){
							lst[i].checked = true
						}
					}
				}else{
					if(lst[i].dictValue == this.data.value){
						lst[i].checked = true
					}
				}
				this.setData({
					list: lst
				})
			}
		}else{
			getDictData(this.data.dictType).then(res=>{
				let items = res.data
				for(const i in items){
					items[i].color = getClassColor(items[i].listClass)
					items[i].checked = false
					if(this.data.multiple){
						for(const c in this.data.values){
							if(items[i].dictValue == this.values[c]){
								items[i].checked = true
							}
						}
					}else{
						if(items[i].dictValue == this.data.value){
							items[i].checked = true
						}
					}
					lst.push(items[i])
					this.setData({
						list: lst
					})
				}
			})
		}
	},
  /**
   * 组件的方法列表
   */
  methods: {
		handleChoose(e){
			let items = this.data.list;
			let values = e.currentTarget.dataset.value;
			for (let i = 0, lenI = items.length; i < lenI; ++i) {
				if (items[i].dictValue == values) {
					items[i].checked = !items[i].checked;
					break
				}
			}
		},
		
		select: function(e){
			let data = {
				action: 'channel'
			}

			if(e==1){
				let lst = []
				for(const i in this.data.list){
					if(this.data.list[i].checked){
						lst.push(this.data.list[i])
					}
				}
				data.action = 'select'
				data.data = lst
			}
			//this.$emit('selectData', data)
			this.triggerEvent('selectData', data)
		}

  }
})
